package com.exmaple.smart.gwt.client;

import com.exmaple.smart.gwt.client.ui.data.NavigationPaneRecord;
import com.exmaple.smart.gwt.client.ui.data.ResourceCentreNavigationPaneSectionData;
import com.exmaple.smart.gwt.client.ui.data.SalesNavigationPaneSectionData;
import com.exmaple.smart.gwt.client.ui.data.SettingsNavigationPaneSectionData;
import com.exmaple.smart.gwt.client.ui.view.AccountView;
import com.exmaple.smart.gwt.client.ui.view.ContextAreaFactory;
import com.exmaple.smart.gwt.client.ui.widgets.ApplicationMenu;
import com.exmaple.smart.gwt.client.ui.widgets.ContextArea;
import com.exmaple.smart.gwt.client.ui.widgets.Masthead;
import com.exmaple.smart.gwt.client.ui.widgets.NavigationPane;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ClientBundle.Source;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */

public class SmartGWTTest implements EntryPoint {

	private static final int NORTH_HEIGHT = 85; // MASTHEAD_HEIGHT +
												// APPLICATION_MENU_HEIGHT
	private static final int DEFAULT_MENU_WIDTH = 70;

	private VLayout mainLayout;
	private HLayout northLayout;
	private HLayout southLayout;
	private VLayout eastLayout;
	private VLayout westLayout;

	ApplicationMenu applicationMenu;

	interface GlobalResources extends ClientBundle {
		@NotStrict
		@Source("SmartGWTTest.css")
		CssResource css();
	}

	public void onModuleLoad() {

		GWT.log("init OnLoadModule()...", null);

		// inject global styles
		GWT.<GlobalResources> create(GlobalResources.class).css()
				.ensureInjected();

		// get rid of scroll bars, and clear out the window's built-in margin,
		// because we want to take advantage of the entire client area
		Window.enableScrolling(false);
		Window.setMargin("0px");

		// initialise the main layout container
		mainLayout = new VLayout();
		mainLayout.setWidth100();
		mainLayout.setHeight100();

		// initialise the North layout container
		northLayout = new HLayout();
		northLayout.setHeight(NORTH_HEIGHT);

		VLayout vLayout = new VLayout();
		// add the Masthead to the nested layout container
		vLayout.addMember(new Masthead());

		// initialise the Application menu
		applicationMenu = new ApplicationMenu();
		applicationMenu.addMenu("<u>N</u>ew Activity", DEFAULT_MENU_WIDTH,
				"Task, Fax, Phone Call, Email, Letter, " + "Appointment",
				new ApplicationMenuClickHandler());
		applicationMenu.addMenu("New Re<u>c</u>ord", DEFAULT_MENU_WIDTH,
				"Account, Contact, separator, Lead, Opportunity",
				new ApplicationMenuClickHandler());
		Menu goToMenu = applicationMenu.addMenu("<u>G</u>o To",
				DEFAULT_MENU_WIDTH - 30);
		applicationMenu.addSubMenu(goToMenu, "Sales",
				"Leads, Opportunities, Accounts, Contacts",
				new ApplicationMenuClickHandler());
		applicationMenu.addSubMenu(goToMenu, "Settings",
				"Administration, Templates, Data Management",
				new ApplicationMenuClickHandler());
		applicationMenu.addSubMenu(goToMenu, "Resource Centre",
				"Highlights, Sales, Settings",
				new ApplicationMenuClickHandler());
		applicationMenu.addMenu("<u>T</u>ools", DEFAULT_MENU_WIDTH - 30,
				"Import Data, Duplicate Detection, Advanced Find, Options",
				new ApplicationMenuClickHandler());
		applicationMenu.addMenu("<u>H</u>elp", DEFAULT_MENU_WIDTH - 30,
				"Help on this Page, Contents, myCRM Online, About myCRM",
				new ApplicationMenuClickHandler());

		// add the Application Menu to the nested layout container
		vLayout.addMember(applicationMenu);

		// add the nested layout container to the North layout container
		northLayout.addMember(vLayout);

		// initialise the Navigation Pane
		NavigationPane navigationPane = new NavigationPane();
		navigationPane.add("Sales",
				SalesNavigationPaneSectionData.getRecords(),
				new NavigationPaneClickHandler());
		navigationPane.add("Settings",
				SettingsNavigationPaneSectionData.getRecords(),
				new NavigationPaneClickHandler());
		navigationPane.add("Resource Centre",
				ResourceCentreNavigationPaneSectionData.getRecords(),
				new NavigationPaneClickHandler());

		// select the first Navigation Pane section e.g Sales
		navigationPane.expandSection(0);

		// initialise the West layout container
		westLayout = navigationPane;

		// initialise the East layout container
		eastLayout = new AccountView();

		// initialise the South layout container
		southLayout = new HLayout();

		// set the Navigation Pane and Context Area as members of the South
		// layout container
		southLayout.setMembers(westLayout, eastLayout);

		// add the North and South layout containers to the main layout
		// container
		mainLayout.addMember(northLayout);
		mainLayout.addMember(southLayout);

		// add the main layout container to GWT's root panel
		RootLayoutPanel.get().add(mainLayout);
	}

	private class ApplicationMenuClickHandler implements ClickHandler {
		@Override
		public void onClick(MenuItemClickEvent event) {
			String applicationName = event.getItem().getTitle();
			SC.say("You clicked: " + applicationName);
		}
	}

	private class NavigationPaneClickHandler implements RecordClickHandler {
		@Override
		public void onRecordClick(RecordClickEvent event) {
			NavigationPaneRecord record = (NavigationPaneRecord) event
					.getRecord();
			setContextAreaView(record);
		}
	}

	private void setContextAreaView(NavigationPaneRecord record) {
		ContextAreaFactory factory = record.getFactory();
		Canvas view = factory.create();
		southLayout.setMembers(westLayout, view);
	}
}